package com.gps.skyrc.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gps.skyrc.R;
import com.gps.skyrc.bean.HistoryData;
import com.gps.skyrc.tool.StaticUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Create by ywq
 * data 2018/12/12
 */
public class HistoryListAdapter extends RecyclerView.Adapter<HistoryListAdapter.HistoryViewHolder> {
    private Context mContext;
    private List<HistoryData> mList;
    private HashMap<Integer, Boolean> mcheckStatue;
    public CallBack mCallBack;
    private boolean isShow = false;

    public HistoryListAdapter(Context context) {
        this.mContext = context;
    }


    public void setCallBackListener(CallBack callBack) {
        mCallBack = callBack;
    }

    public interface CallBack {
        void onMoreItemClick(HashMap<Integer, Boolean> checkStatue);

        void onSingleItemClick(HistoryData bean);
    }

    @NonNull
    @Override
    public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = View.inflate(mContext, R.layout.history_list_item, null);
        return new HistoryViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
        HistoryData bean = mList.get(position);
        holder.mModeTv.setText(bean.getMode() == 0 ? mContext.getString(R.string.speed_mode) :
                bean.getMode() == 1 ? mContext.getString(R.string.distance_mode) :
                        bean.getMode() == 2 ? mContext.getString(R.string.track_test) : mContext.getString(R.string.fly_test));
        holder.mTimeTv.setText(StaticUtils.timesDate(bean.getTimestamp(), "yyyy.MM.dd HH:mm"));
        holder.mDataValid.setText(bean.isValid() ? mContext.getString(R.string.valid_data) : mContext.getString(R.string.invalid_data));
        if (isShow) {
            holder.mButton.setVisibility(View.VISIBLE);
        } else {
            holder.mButton.setVisibility(View.GONE);
        }

        holder.mButton.setChecked(mcheckStatue.get(position));


        holder.mButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (buttonView.isPressed()) {
                mcheckStatue.put(position, isChecked);
            }
        });

        holder.mItems.setOnClickListener(v -> {
            if (isShow) {//在删除中
                if (holder.mButton.isChecked()) {
                    holder.mButton.setChecked(false);
                    mcheckStatue.put(position, false);
                } else {
                    holder.mButton.setChecked(true);
                    mcheckStatue.put(position, true);
                }
                if (null != mCallBack)
                    mCallBack.onMoreItemClick(mcheckStatue);
            } else {
                //跳转  详细页面
                if (null != mCallBack)
                    mCallBack.onSingleItemClick(bean);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setDatas(ArrayList<HistoryData> list, HashMap<Integer, Boolean> checkStatue) {
        this.mList = list;
        this.mcheckStatue = checkStatue;
        notifyDataSetChanged();
    }

    public void setIsShow(boolean show) {
        isShow = show;
        notifyDataSetChanged();
    }


    class HistoryViewHolder extends RecyclerView.ViewHolder {

        private final TextView mModeTv;
        private final TextView mTimeTv;
        private final CheckBox mButton;
        private final RelativeLayout mItems;
        private final TextView mDataValid;

        public HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            mItems = itemView.findViewById(R.id.items_rl);
            mModeTv = itemView.findViewById(R.id.mode_tv);
            mTimeTv = itemView.findViewById(R.id.times_tv);
            mButton = itemView.findViewById(R.id.img_button);
            mDataValid = itemView.findViewById(R.id.data_valid);
        }
    }
}
